﻿// Copyright 2010 Maxime Serrano <lastchronicler@gmail.com>
// Copyright 2009, 2010 Matvei Stefarov <me@matvei.org>
using System;

namespace ForgeCraft
{
    public class CmdCompass : Command
    {
        public override string name { get { return "compass"; } }
        public override string shortcut { get { return "comp"; } }
        public override string type { get { return "general"; } }
        public override bool consoleUsable { get { return false; } }
        public override Permission defaultRank { get { return Permission.Guest; } }
        public CmdCompass() { }

        public override void Use(Player p, string message)
        {
            string compass = "N . . . nw. . . W . . . sw. . . S . . . se. . . E . . . ne. . . " +
                             "N . . . nw. . . W . . . sw. . . S . . . se. . . E . . . ne. . . ";

            Player target = p;

            if (message != null)
            {
                target = Server.FindPlayer(message);
                if (target != null)
                {
                    p.Message("Coordinates of player \"" + target.nick + "\":");
                }
                else
                {
                    p.NoPlayerMessage(message);
                    return;
                }
            }
            else if (p.session == null)
            {
                p.Message("When called from console, " + Color.Help + "/compass" + Color.Sys + " requires a player name.");
                return;
            }

            int offset = (int)(((360f - target.pos.rotation) - 90f) / 360f * 64f) + 32;

            p.Message(Color.Silver + String.Format("( {0}, {1}, {2} ) - {3}[{4}{5}{6}{3}{7}]",
                            target.pos.x.ToString("N2"),
                            target.pos.y.ToString("N2"),
                            target.pos.z.ToString("N2"),
                            Color.White,
                            compass.Substring(offset - 12, 11),
                            Color.Red,
                            compass.Substring(offset - 1, 3),
                            compass.Substring(offset + 2, 11)));
        }
        public override void Help(Player p)
        {
            p.Message(Color.Aqua + "/compass " + Color.Help + "- Shows information about the location and orientation of a player.");
            p.Message("If no name is given, shows player's own info.");
        }
    }
}